<template>
  <page-header-wrapper :title="false">
    <a-card title="活动详情">
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="活动名称" :span="3"> {{ info?.name }} </a-descriptions-item>
        <a-descriptions-item label="创建人" :span="3"> {{ info?.creatorName }} </a-descriptions-item>
        <a-descriptions-item label="开始时间" :span="3"> {{ info?.beginDate }} </a-descriptions-item>
        <a-descriptions-item label="结束时间" :span="3"> {{ info?.endDate }}  </a-descriptions-item>
        <a-descriptions-item label="持续天数" :span="3"> {{ info?.days }} </a-descriptions-item>
        <a-descriptions-item label="循环活动" :span="3"> {{ info?.isRepeat == 0 ? '否': '是' }} </a-descriptions-item>
        <a-descriptions-item label="赠送积分" :span="3"> {{ info?.sendIntegral }} </a-descriptions-item>
        <a-descriptions-item label="状态" :span="3">
          <!-- {{ info?.status == '1' ? '进行中' : '已结束' }} -->
          <div v-if="info.status == '0'">已结束</div>
          <div v-if="info.status == '1'">正在进行</div>
          <div v-if="info.status == '2'">未开启</div>
        </a-descriptions-item>
        <a-descriptions-item label="类型" :span="3">
          <div v-if="info?.actType == 0 ">私密活动 
            <a-button type="primary" style="margin: 0px 5px;background-color:#87B87F!important;border-color:#87B87F" @click="handlePresentScoreActivity(info)" v-if="info?.status == 1">赠送积分</a-button></div>
          <div v-if="info?.actType == 1 ">公开活动</div>
        </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex">
            <div v-if="info?.status == '0'">无</div>
            <a-button type="primary" style="margin: 0px 5px" @click="handleStart(info)" v-if="info?.status === '2'" >开启活动</a-button>
            <a-button type="danger" style="background-color: #D15B47!important;border-color: #D15B47!important" @click="handleEnd(info)" v-if="info?.status === '1'">结束活动</a-button>
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="轮次列表" style="margin-top: 24px">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="searchLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :loading="searchLoading"
        :data-source="loadData"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handleDetail(record)">进入</a-tag>
          </template>
        </span>
      </a-table>
    </a-card>
  
  </page-header-wrapper>
</template>

<script>
import {
  getMemActivityUsingPOST,
  getMemActivityRoundListUsingPOST,
  endMemActivityUsingPOST,
  addMemActivityUsingPOST1,
  exportMemActivityRoundListUsingPOST
} from '@/api/api/huiyuanguanli.ts'
export default {
  name: 'MemberListRoundDetail',
  components: {},
  data() {
    return {
      info: {},
      advanced: false,
      priceMdll: null,
      editMdll: null,
      priceVisible: false,
      editVisible: false,
      priceLoading: false,
      editLoading: false,
      searchLoading: false,
      recordLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      pagination: {},
      columns: [
        {
          title: '轮次编号',
          dataIndex: 'id',
        },
        {
          title: '开始时间',
          dataIndex: 'startTime',
        },
        {
          title: '结束时间',
          dataIndex: 'endTime',
        },
        {
          title: '参与人次',
          dataIndex: 'joinNum',
        },
        {
          title: '送出积分',
          dataIndex: 'giveIntegral',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
    }
  },
  mounted() {
    this.getDetail()
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleExpert(param){
      exportMemActivityRoundListUsingPOST({...param, orderId: this.$route.params.id}).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '轮次列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handleStart(info){
      const that = this
      this.$confirm({
        title: '开启活动',
        content: '你确定要开启活动吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          addMemActivityUsingPOST1({ id: info.id }).then(() => { 
            that.getDetail(that.queryParam)
            that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleEnd(info){
      const that = this
      this.$confirm({
        title: '结束活动',
        content: '你确定要结束活动吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          endMemActivityUsingPOST({ id: info.id }).then(() => { 
            that.getDetail(that.queryParam)
            that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handlePresentScoreActivity(record) {
      this.$router.push({ path: `/member/list/scoreactivity/presentScoreActivity/${record.id}`})
    },
    handleDetail(record) {
      this.$router.push({ path: `/member/list/scoreactivity/roundDetail/${record.activityId}`,query:{activity:record.id } })
    },
    getDetail() {
      getMemActivityUsingPOST({ id: this.$route.params.id })
        .then((res) => {
          this.info = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(param) {
      this.searchLoading = true
      getMemActivityRoundListUsingPOST({ ...param, orderId: this.$route.params.id })
        .then((res) => {
          this.searchLoading = false
          this.loadData = res.data.list
          this.queryParam.pageNo = this.pagination.current
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.searchLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: this.queryParam.queryCondition,
        status: this.queryParam.status,
        date: this.queryParam.date,
      })
    },
  },
}
</script>
